package com.jake.spring.lifecycle.config;

import com.jake.spring.lifecycle.domain.Person;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;

@Configuration
@Slf4j
public class PersonConfig {

    @Bean(initMethod = "customInit", destroyMethod = "customDestroy")
    public Person singletonPerson() {
        log.info("singleton person start");
        Person singletonPerson = new Person();
        singletonPerson.setName("Singleton Jake");
        log.info("singleton person end");
        return singletonPerson;
    }

    @Bean(initMethod = "customInit", destroyMethod = "customDestroy")
    @Lazy // 懒加载：调用bean的时候才初始化，单例
    public Person lazySingletonPerson() {
        Person lazySingletonPerson = new Person();
        lazySingletonPerson.setName("Lazy Singleton Jake");
        return lazySingletonPerson;
    }

    @Bean(initMethod = "customInit", destroyMethod = "customDestroy")
    @Scope("prototype") // 原型：调用bean的时候才初始化，多例，即每次获取的bean都不相同
    public Person prototypePerson() {
        Person prototypePerson = new Person();
        prototypePerson.setName("Prototype Jake");
        return prototypePerson;
    }
}
